package org.begincode.admin.control;

import com.begincode.common.constant.Constant;
import com.begincode.common.util.ResultUtil;
import org.begincode.admin.service.CodeService;
import org.begincode.core.model.CodeComment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by yangsj on 2016/4/24.
 * 代码管理
 */
@Controller
@RequestMapping("codeManage")
public class CodeController {
    Logger logger = LoggerFactory.getLogger(CodeController.class);
    @Resource
    private CodeService codeService;

    /**
     * 查看未审核代码评论
     * */
    @ResponseBody
    @RequestMapping(value = "selUnCheckCodeComments")
    public Object selUnCheckCodeComments(){
        logger.info("selUnCheckCodeComments");
        List<CodeComment> codeComments =  codeService.selUnCheckCodeComment();
        return ResultUtil.buildSuccess(codeComments);
    }
    /**
     * 通过代码评论
     * */
    @ResponseBody
    @RequestMapping(value = "passCodeComment/{codeCommentId}")
    public Object passBlogComment(@PathVariable("codeCommentId") int codeCommentId){
        logger.info("passCodeComment codeCommentId = {}", codeCommentId);
        CodeComment codeComment = new CodeComment();
        codeComment.setCodeCommentId(codeCommentId);
        codeComment.setCheckFlag(Constant.CHECKED);
        int updateCount =  codeService.updateCodeCommentById(codeComment);
        if(updateCount == 1){
            return ResultUtil.buildSuccess();
        }
        return ResultUtil.buildFail();
    }
    /**
     * 删除代码评论
     * */
    @ResponseBody
    @RequestMapping(value = "delCodeComment/{codeCommentId}")
    public Object delCodeComment(@PathVariable("codeCommentId") int codeCommentId){
        logger.info("delCodeComment codeCommentId = {}", codeCommentId);
        CodeComment codeComment = new CodeComment();
        codeComment.setCodeCommentId(codeCommentId);
        codeComment.setCommentStatus(Constant.DEL_FLAG);
        codeComment.setCheckFlag(Constant.CHECKED);
        int updateCount =  codeService.updateCodeCommentById(codeComment);
        if(updateCount == 1){
            return ResultUtil.buildSuccess();
        }
        return ResultUtil.buildFail();
    }
}
